/**
 * Trim all fore and aft spaces & CR, LF, etc
 * @param string str The string to trim
 * @param string chars Optional characters to remove from string
 */
function trim(str,chars) {
  return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str,chars) {
  chars = chars || "\\s";
  return str.replace(new RegExp("^["+chars+"]+","g"),"");
}
 
function rtrim(str,chars) {
  chars = chars || "\\s";
  return str.replace(new RegExp("["+chars+"]+$","g"),"");
}

/**
 * Center a div to screen
 * @param string div_id The name of div to center
 */
function center_div(div_id,width,height) { 
  var viewportwidth;
  var viewportheight;

  //the more standards compliant browsers (mozilla/netscape/opera/IE7) use 
  //window.innerWidth and window.innerHeight
  if (typeof(window.innerWidth)!="undefined") {
    viewportwidth = window.innerWidth;
    viewportheight = window.innerHeight;
  }
 
  //IE6 in standards compliant mode (i.e. with a valid doctype as the 
  //first line in the document)
  else 
  if (typeof(document.documentElement)!="undefined"
      && typeof(document.documentElement.clientWidth) !=
      "undefined" && document.documentElement.clientWidth != 0) {
    viewportwidth = document.documentElement.clientWidth;
    viewportheight = document.documentElement.clientHeight;
  }

  //older versions of IE
  else {
    viewportwidth = document.getElementsByTagName("body")[0].clientWidth;
    viewportheight = document.getElementsByTagName("body")[0].clientHeight;
  }

  //set position of div_id
  scroll_top = document.body.scrollTop;
  scroll_left = document.body.scrollLeft;
  
  //set dimmer div
  var rim = 20;
  document.getElementById("div_dialog_dimmer").style.width = width+2*rim;
  document.getElementById("div_dialog_dimmer").style.height = height+2*rim;
  document.getElementById(div_id).style.zIndex = 65535;
  
  //set dimmer div position from top -- DIMMER TOP
  if (typeof(document.getElementById("div_dialog_dimmer").style.posTop)!=
      "undefined")
    document.getElementById("div_dialog_dimmer").style.posTop=
        (viewportheight-height-2*rim)/2 + scroll_top;
  else
    document.getElementById("div_dialog_dimmer").style.top=
        (viewportheight-height-2*rim)/2 + scroll_top;  
    
  //set dimmer position from left -- DIMMER LEFT
  if (typeof(document.getElementById("div_dialog_dimmer").style.posLeft)!=
      "undefined")  
    document.getElementById("div_dialog_dimmer").style.posLeft=
        (viewportwidth-width-2*rim)/2 + scroll_left;
  else
    document.getElementById("div_dialog_dimmer").style.left=
        (viewportwidth-width-2*rim)/2 + scroll_left;  
  
  //set content div position from top -- CONTENT TOP
  if (typeof(document.getElementById(div_id).style.posTop)!="undefined")
    document.getElementById(div_id).style.posTop=
        (viewportheight-height)/2 + scroll_top;
  else
    document.getElementById(div_id).style.top=
        (viewportheight-height)/2 + scroll_top;  
    
  //set content position from left -- CONTENT LEFT
  if (typeof(document.getElementById(div_id).style.posLeft)!="undefined")  
    document.getElementById(div_id).style.posLeft=
        (viewportwidth-width)/2 + scroll_left;
  else
    document.getElementById(div_id).style.left=
        (viewportwidth-width)/2 + scroll_left;
  
  //window.onresize = center_div(div_id,width,height);
  //window.onscroll = center_div(div_id,width,height);
}

function focus_first_input_inside(div_id) {
  //
}

var cornice_dialog_counter = 0;

function open_dialog(div_id,width,height) {
  if (cornice_dialog_counter>0)
    return;
    
  //document.getElementById(div_id).style.overflow = "auto";
  document.getElementById(div_id).style.display = "block";
  document.getElementById(div_id).style.position = "absolute";
  document.getElementById(div_id).style.visibility = "visible";
  center_div(div_id,width,height);
  cornice_dialog_counter = 1;
  
  focus_first_input_inside(div_id);
}

function close_dialog(div_id) {
  document.getElementById(div_id).style.display = "none";
  document.getElementById("div_dialog_dimmer").style.width = 0;
  document.getElementById("div_dialog_dimmer").style.height = 0;  
  cornice_dialog_counter = 0;
}

function expand_div(div_id) {
  document.getElementById(div_id).style.display = "block";
  document.getElementById(div_id).style.visibility = "visible";  
}

function collapse_div(div_id) {
  document.getElementById(div_id).style.display = "none";
}

function show_div(div_id) {
  document.getElementById(div_id).style.display = "block";
  document.getElementById(div_id).style.position = "absolute";
  document.getElementById(div_id).style.visibility = "visible";
}

function hide_div(div_id) {
  document.getElementById(div_id).style.display = "none";
}